<template>
  <Dialog v-model="dialogVisible" :title="dialogTitle" width="1200px">
    <!-- 搜索 -->
    <ContentWrap>
      <el-form
        class="-mb-15px"
        :model="queryParams"
        ref="queryFormRef"
        :inline="true"
        label-width="80px"
      >
        <el-form-item label="类型" prop="productTypeSn">
            <el-select
              v-model="queryParams.productTypeSn"
              placeholder="请输入类型SN"
              clearable
              class="!w-120px"
            >
              <el-option
                v-for="dict in getStrDictOptions(DICT_TYPE.METER_PRODUCT_TYPE)"
                :key="(dict.value)"
                :label="dict.label"
                :value="(dict.value)"
              />
            </el-select>
        </el-form-item>
        <el-form-item label="名称" prop="name">
          <el-input
            v-model="queryParams.name"
            placeholder="请输入名称"
            clearable
            @keyup.enter="handleQuery"
            class="!w-240px"
          />
        </el-form-item>
        <el-form-item>
          <el-button @click="handleQuery"><Icon icon="ep:search" class="mr-5px" /> 搜索</el-button>
        </el-form-item>
      </el-form>
    </ContentWrap>
    <ContentWrap>
      <el-table v-loading="loading" :data="list">
        <el-table-column type="expand">
        <template #default="props">
          <div m="4">
            <p m="t-0 b-2">厂家: {{ props.row.manufacturerAme }}</p>
            <p m="t-0 b-2">厂家SN: {{ props.row.manufacturerSn }}</p>
            <p m="t-0 b-2">通信协议: {{ props.row.protocolTypeName }}</p>
            <p m="t-0 b-2">通信协议SN: {{ props.row.protocolTypeIdSn }}</p>
            <p m="t-0 b-2">通信方式: {{ props.row.communicationModeSnName }}</p>
            <p m="t-0 b-2">通信方式SN: {{ props.row.communicationModeSn }}</p>
            <p m="t-0 b-2">型号编码: {{ props.row.productNumber }}</p>
            <p m="t-0 b-2">采集器类型: {{ props.row.collectorTypeShow }}</p>
            <p m="t-0 b-2">采集器类型代码: {{ props.row.collectorType }}</p>
            <p m="t-0 b-2">电表类别: {{ props.row.deviceCategoryName }}</p>
            <p m="t-0 b-2">电表类别代码: {{ props.row.deviceCategory }}</p>
            <p m="t-0 b-2">电表类型: {{ props.row.electricPriceTypeName }}</p>
            <p m="t-0 b-2">电表类型代码: {{ props.row.electricPriceType }}</p>
          </div>
        </template>
      </el-table-column>
        <!-- <el-table-column label="主键" align="center" prop="id" width="180"/> -->
        <el-table-column label="类型" align="center" prop="productTypeName" width="80"/>
        <!-- <el-table-column label="类型SN" align="center" prop="productTypeSn" width="80"/> -->
        <!-- <el-table-column label="厂家" align="center" prop="manufacturerAme" />
        <el-table-column label="厂家SN" align="center" prop="manufacturerSn" /> -->
        <el-table-column label="型号名称" align="left" prop="name" />
        <el-table-column label="通信协议" align="center" prop="protocolTypeName" width="120"/>
        <!-- <el-table-column label="通信协议SN" align="center" prop="protocolTypeIdSn" /> -->
        <el-table-column label="通信方式" align="center" prop="communicationModeSnName" width="80"/>
        <!-- <el-table-column label="通信方式SN" align="center" prop="communicationModeSn" /> -->
        <!-- <el-table-column label="型号编码" align="center" prop="productNumber" /> -->
        <!-- <el-table-column label="采集器类型" align="center" prop="collectorTypeShow" width="170"/> -->
        <!-- <el-table-column label="采集器类型代码" align="center" prop="collectorType" /> -->
        <el-table-column label="电表类别" align="center" prop="deviceCategoryName" width="80"/>
        <!-- <el-table-column label="电表类别代码" align="center" prop="deviceCategory" /> -->
        <el-table-column label="电表类型" align="center" prop="electricPriceTypeName" width="170"/>
        <!-- <el-table-column label="电表类型代码" align="center" prop="electricPriceType" /> -->
        <el-table-column
          label="操作"
          align="center"
          class-name="small-padding fixed-width"
          width="120"
        >
          <template #default="scope">
            <el-button type="primary" link @click="confirmProduct(scope.row)">选择</el-button>
          </template>
        </el-table-column>
      </el-table>
      <Pagination
        :total="total"
        v-model:page="queryParams.pageNo"
        v-model:limit="queryParams.pageSize"
        @pagination="getList"
      />
    </ContentWrap>
  </Dialog>
</template>
<script lang="ts" setup>
import { DICT_TYPE, getStrDictOptions } from '@/utils/dict'
import { MeterHxnyDictApi } from '@/api/zwsj/meterhxnydict'

defineOptions({ name: 'HxnyProductDict' })

const loading = ref(true) // 列表的加载中
const total = ref(0) // 列表的总页数
const list = ref([]) // 列表的数
const dialogVisible = ref(false) // 弹窗的是否展示
const dialogTitle = ref('') // 弹窗的标题
const showHistoryFlag = ref(false)

const queryParams = reactive({
  pageNo: 1,
  pageSize: 10,
  productTypeSn: '1010',
  name: undefined
})

const queryFormRef = ref() // 搜索的表单

/** 打开弹窗 */
const open = async () => {
  dialogVisible.value = true
  dialogTitle.value = '宏新能源电表型号选择'
  showHistoryFlag.value = false
  getList()
}
defineExpose({ open }) // 提供 open 方法，用于打开弹窗

/** 查询列表 */
const getList = async () => {
  loading.value = true
  try {
      const data = await MeterHxnyDictApi.getMeterHxnyDictPage(queryParams)
      list.value = data.list
      total.value = data.total
  } finally {
    loading.value = false
  }
}

/** 确认 */
const emits = defineEmits(['confirmProductDict'])
const confirmProduct = async (row: any) => {
  emits('confirm-product-dict', row)
  dialogVisible.value = false
}

/** 搜索按钮操作 */
const handleQuery = () => {
  queryParams.pageNo = 1
  getList()
}
</script>
